package com.leo.rocketmqproducer;

import com.leo.common.constants.RocketMQConstant;
import com.leo.common.request.PraiseRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 点赞
 *
 * @author Leo
 * @version V1.0
 */
//@RestController
@ResponseBody
@Controller
@Slf4j
public class PraiseController {

    private final RocketMQTemplate rocketMQTemplate;

    @Autowired
    public PraiseController(RocketMQTemplate rocketMQTemplate) {
        this.rocketMQTemplate = rocketMQTemplate;
    }

    /**
     *
     * RocketMQ的消息发送方式主要含syncSend()同步发送、asyncSend()异步发送、sendOneWay()三种方式，
     * sendOneWay()也是异步发送，区别在于不需等待Broker返回确认，所以可能会存在信息丢失的状况，但吞吐量更高，具体需根据业务情况选用。
     * 性能：sendOneWay > asyncSend > syncSend RocketMQTemplate的send()方法默认是同步(syncSend)的
     *
     * @param praiseRequestDTO
     * @return
     */
    @PostMapping(value = "/praise")
    public ResponseEntity praise(@RequestBody PraiseRequestDTO praiseRequestDTO) {
        log.info("点赞请求参数：{}", praiseRequestDTO);
        rocketMQTemplate.sendOneWay(RocketMQConstant.TOPIC_PRAISE, MessageBuilder.withPayload(praiseRequestDTO).build());
        return ResponseEntity.ok(Boolean.TRUE);
    }

}
